//
// Created by rengen.gong on 2021/12/7.
//

#ifndef LEARN1_ARRAYLIST_H
#define LEARN1_ARRAYLIST_H

#include "iostream"
#include "stdlib.h"

template<class T>
int getArrayLen(T &array) {
    return (sizeof(array) / sizeof(array[0]));
}


template<class C>
class ArrayList {
private:
    int size = 8;
    int actualCount = 0;
    C *list = 0;

    void growCapacity() {
        size *= 2;
        C growList[size];
        memcpy(growList, list, size);
        list = growList;
    }

    void checkCapacity() {
        if (list == 0) {
            C mList[size];
            list = mList;
        }

        if (actualCount >= size * 0.75) {
            growCapacity();
        }
    }

public:

    int count() const {
        return actualCount;
    }

    void add(C c) {
        checkCapacity();
        list[actualCount++] = c;
    }

    C get(int position) const {
        return list[position];
    }

    void print() {
        for (int i = 0; i < count(); i++) {
            cout << get(i) << endl;
        }
        cout << "==========" << endl;

    }

};

#endif //LEARN1_ARRAYLIST_H
